#include "stdafx.h"
#include "DiagramVideo.h"

#include <QCoreApplication>
#include <QGraphicsScene>
#include <QPainter>
#include <QDebug>
#include <QtWinExtras/QtWin>

#include <gdiplus.h>

DiagramVideo::DiagramVideo(QObject *parent)
{
	setFlag(QGraphicsItem::ItemSendsGeometryChanges,true);
	m_nCameraImgW = m_nCameraImgH = 0;
	t_nScreenW = t_nScreenH = 0;
	m_nScreenW = m_nScreenH = 0;
	t_nCameraImgW = t_nCameraImgH = 0;
	m_nCameraImgW = m_nCameraImgH = 0;
	m_pPaintThread = NULL;
	m_staticmap = QPixmap(":/imgview/offline");
}

DiagramVideo::~DiagramVideo()
{
	stopPlayCamera();
}

void  DiagramVideo::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget  )
{
	m_nScreenW = m_rtScreenRect.width();
	m_nScreenH = m_rtScreenRect.height();

	QPixmap pixmap;
	if(!m_threadimg.isNull() && m_nCameraImgW != 0 && m_nCameraImgH != 0)
	{	
		getMatchSize(m_rtDisplay);
		pixmap = QPixmap::fromImage(m_threadimg.scaled(QSize(m_rtDisplay.size()),Qt::KeepAspectRatio));
	}

#if 0
	if (!m_threadpixmap.isNull() && m_nCameraImgW != 0 && m_nCameraImgH != 0)
	{
		fscaledH = (m_nScreenW * m_nCameraImgH) /  m_nCameraImgW;
		pixmap = m_threadpixmap.scaled(QSize(m_nScreenW,fscaledH),Qt::KeepAspectRatio);		
	}
#endif

	if(m_rtDisplay.isValid() && !m_uCameraID.isNull())
		painter->drawPixmap(m_rtDisplay,pixmap);

	if(isChangeScreenSize())
	{
		t_nScreenH = m_nScreenH;
		t_nScreenW = m_nScreenW;
		//setGeometry(0,0,m_nScreenW,m_nScreenH);
	}	

	 if(m_uCameraID.isNull())
	{
		int nImgH = m_staticmap.height();
		int nImgW = m_staticmap.width();
		int nScaledH;
		if(nImgW * nImgH > 0) 
		{
			nScaledH = (m_nScreenW * nImgH) /  nImgW;
			pixmap = m_staticmap.scaled(QSize(m_nScreenW,nScaledH),Qt::KeepAspectRatio);
		}	

		int nCenterY = 0;
		if(nScaledH <= m_nScreenH)
		{
			nCenterY = qAbs((m_nScreenH - nScaledH)/2);
			setSceneRect(QRectF(0,0,m_nScreenW,m_nScreenH));
		}
		else
			setSceneRect(QRectF(0,0,m_nScreenW,nScaledH));
			//setGeometry(0,0,m_nScreenW,nScaledH);
		
		painter->drawPixmap(QRect(0,nCenterY,m_nScreenW,nScaledH),pixmap);
		return QGraphicsWidget::paint(painter,option,widget);
	}

	return QGraphicsWidget::paint(painter,option,widget);
}

bool DiagramVideo::startPlayCamera( )
{
	if(!m_pPaintThread)  m_pPaintThread = new QPaintThread(this);

	m_pPaintThread->setCameraUuid(m_uCameraID);
	if(m_pPaintThread->isRunning())  return false;
	connect(m_pPaintThread,SIGNAL(changesizeSignal(QSize)),this,SLOT(changesizeSlot(QSize)));

	m_pPaintThread->start();

	return true;
}

bool DiagramVideo::stopPlayCamera()
{
	if(m_pPaintThread)
		m_pPaintThread->stop();
	FREEMEMORY(m_pPaintThread);

	return true;
}


bool DiagramVideo::getMatchSize( QRect& rt )
{
	m_nScreenW = m_rtScreenRect.width();
	m_nScreenH = m_rtScreenRect.height();
	int  nScaleW = m_nScreenW,nScaledH = m_nScreenH,nCenterX = 0,nCenterY = 0;

	bool  bChangeSize = false;
	if(isChangeCameraSize() || isChangeScreenSize())
	{
		bChangeSize = true;
		t_nCameraImgW = m_nCameraImgW;
		t_nCameraImgH = m_nCameraImgH;
	}

	if(m_nCameraImgW != 0 && m_nCameraImgH != 0 && bChangeSize)
	{
		if(m_nCameraImgW >= m_nCameraImgH)
		{
			nScaledH = (m_nScreenW * m_nCameraImgH) / m_nCameraImgW;

			if(nScaledH > m_nScreenH)
			{
				//nScaleW = (m_nScreenW * nScaledH) / m_nScreenH;
				//setGeometry(0,0,m_nScreenW,nScaledH);
				setSceneRect(QRectF(0,0,m_nScreenW,nScaledH));
			}
			else
			{
				//setGeometry(0,0,m_nScreenW,m_nScreenH);
				setSceneRect(QRectF(0,0,m_nScreenW,m_nScreenH));
				nCenterY = qAbs((m_nScreenH - nScaledH)/2);
			}
		}
		else if(m_nCameraImgW < m_nCameraImgH)
		{
			nScaleW = (m_nScreenH * m_nCameraImgW) / m_nCameraImgH;

			if(nScaleW > m_nScreenW)
			{
				//nScaledH = (m_nScreenH * nScaleW) / m_nScreenW;
				//setGeometry(0,0,nScaleW,m_nScreenH);
				setSceneRect(QRectF(0,0,nScaleW,m_nScreenH));
			}
			else
			{
				//setGeometry(0,0,m_nScreenW,m_nScreenH);
				setSceneRect(QRectF(0,0,m_nScreenW,m_nScreenH));
				nCenterX = qAbs((m_nScreenW - nScaleW)/2);
			}
		}
		else 
		{

		}


		rt.setTopLeft(QPoint(nCenterX,nCenterY));
		rt.setSize(QSize(nScaleW,nScaledH));
	}

	return true;
}

bool DiagramVideo::isChangeScreenSize()
{
	if(m_nScreenH!=t_nScreenH || m_nScreenW != t_nScreenW)
		return true;
	return false;
}

bool DiagramVideo::isChangeCameraSize()
{
	if(t_nCameraImgH != m_nCameraImgH || t_nCameraImgW != m_nCameraImgW)
		return true;
	return false;
}

bool DiagramVideo::setSceneRect( QRectF& rt )
{
	DiagramItemEvent  gve;
	gve.msg.message =  DK_COMMAND_SETVIEWSCROLLBAR;
	gve.msg.lParam = (LPARAM)&rt;
	setGeometry(rt);
	QCoreApplication::sendEvent(scene(),&gve);
	return true;
}

void DiagramVideo::changesizeSlot( QSize sz)
{
	m_nCameraImgW = sz.width();
	m_nCameraImgH = sz.height();

	update();
}

bool DiagramVideo::event(QEvent * e )
{
	if(e->type() == WM_NOTIFY_WND)
	{
		DiagramItemEvent* gve = static_cast<DiagramItemEvent* >(e);

		if(gve->msg.message == DK_COMMAND_SETCAMERAUUID)	
		{
			QUuid* uuid = (QUuid*)(gve->msg.lParam);
			m_uCameraID = *uuid;
		}

		else if(gve->msg.message == DK_COMMAND_RESIZEVIEW)	
		{
			QRect* pRect = (QRect*)gve->msg.lParam;
			m_rtScreenRect = *pRect;
		}

		else if(gve->msg.message == DK_COMMAND_PLAYCAMERA)
		{
			QUuid* uuid = (QUuid*)(gve->msg.wParam);
			if(uuid && *uuid == m_uCameraID)
				startPlayCamera();
		}
	}

	return QGraphicsWidget::event(e);
}

//////////////////////////////////////////////////////////////////////////
QPaintThread::QPaintThread( QObject* parent /*= 0*/ )
	:QThread(parent),m_pParent(parent)
{
	m_bRunFlag = true;

	m_pNewPixmap = new QPixmap;

	m_pImage = new QImage;

	m_byRaw = NULL;
}

QPaintThread::~QPaintThread()
{
	FREEMEMORY(m_pNewPixmap);

	FREEMEMORY(m_pImage);
}

void QPaintThread::stop()
{
	m_bRunFlag = false;

	wait();
}

void QPaintThread::setCameraUuid( QUuid& uuid )
{
	m_uCameraID = uuid;
}

void QPaintThread::run()
{
	
	while (m_bRunFlag)
	{
		BYTE*  byBMPBuf = NULL;
		int nSize = 0;


// 		if(g_pDKGuiCenterApi && g_pDKCenterCameraApi->getCameraBMPBuffer )
// 			nSize = g_pDKCenterCameraApi->getCameraBMPBuffer(m_uCameraID,&byBMPBuf);

		//int nCameraImgW = 0,nCameraImgH = 0;
		QSize  szCameraImg;
		//g_pDKCenterCameraApi->getCameraImageSize(m_uCameraID,szCameraImg);

		if(byBMPBuf == NULL)  continue;

		if(nSize > 0 && byBMPBuf)
		{
#if 0
			m_pNewPixmap ->loadFromData(byBMPBuf,nSize);

			((DiagramVideo*)m_pParent)->m_threadpixmap = m_pNewPixmap->copy(0,0,m_pNewPixmap->width(),m_pNewPixmap->height());

			emit changesizeSignal(QSize(m_pNewPixmap->width(),m_pNewPixmap->height()));
#endif
			
#if 1
			m_pImage->loadFromData(byBMPBuf,nSize);
			((DiagramVideo*)m_pParent)->m_threadimg = m_pImage->copy(0,0,m_pImage->width(),m_pImage->height());
			//((DiagramVideo*)m_pParent)->m_threadimg = m_pImage->copy(0,0,szCameraImg.width(),szCameraImg.height());
			emit changesizeSignal(szCameraImg);

			if(0)
			{
				m_pImage->save("111.bmp");
			}
#endif
		} 

		
	}

	FREEMEMORY(m_pNewPixmap);
}
